<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="__PUBLIC__/layui/css/layui.css"  media="all">
    <!-- 注意：如果你直接复制所有代码到本地，上述css路径需要改成你本地的 -->
    <style type="text/css">
        .layui-upload-img{
            /*width: 100px;*/
        }
    </style>
</head>
<body style="padding:15px;">
<form class="layui-form" action="">
    <input type="hidden" name="id" value="{$vo.id}">
    <div class="layui-form-item">
        <label class="layui-form-label">名称</label>
        <div class="layui-input-block">
            <input type="text" name="title" lay-verify="title" autocomplete="off" placeholder="请输入标题" class="layui-input" value="{$vo.title}">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">位置</label>
        <div class="layui-input-block">
            <input type="radio" name="position" value="1" title="首页" <if condition="$vo.position eq 1 ">checked</if>>
            <input type="radio" name="position" value="2" title="好店" <if condition="$vo.position eq 2 ">checked</if>>
            <input type="radio" name="position" value="3" title="同城圈" <if condition="$vo.position eq 3 ">checked</if>>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">链接类型</label>
        <div class="layui-input-block">
            <input type="radio" lay-filter="url_type" name="url_type" value="1" title="小程序链接" <if condition="$vo.url_type eq 1 ">checked</if>>
            <input type="radio" lay-filter="url_type" name="url_type" value="2" title="WEB网页链接" <if condition="$vo.url_type eq 2 ">checked</if>>
        </div>
    </div>
    <div class="layui-form-item jump_type" <if condition="$vo.url_type eq 2 ">style="display: none;"</if>>
        <label class="layui-form-label">广告类型</label>
        <div class="layui-input-block">
            <input type="radio" lay-filter="type" class="type" name="type" value="1" title="跳转店铺" <if condition="$vo.type eq 1 ">checked</if>>
            <input type="radio" lay-filter="type" class="type" name="type" value="2" title="跳转商品" <if condition="$vo.type eq 2 ">checked</if>>
            <input type="radio" lay-filter="type" class="type" name="type" value="3" title="跳转资讯" <if condition="$vo.type eq 3 ">checked</if>>
        </div>
    </div>


    <div class="layui-form-item">
        <label class="layui-form-label">分类图片</label>
        <div class="layui-input-block">
            <div class="layui-upload">
                <button type="button" class="layui-btn layui-btn-danger" id="test1"><i class="layui-icon"></i>上传图片</button>
                <div class="layui-upload-list">
                    <img class="layui-upload-img " id="demo1" src="{$vo.imgurl}" style="width: 80px; height: 80px;<if condition="!$vo['imgurl']">display: none;</if>">
                    <p id="demoText"></p>
                </div>
            </div>
            <input type="hidden" id="imgurl" name="imgurl" value="{$vo.imgurl}">
        </div>
    </div>
    <div class="layui-form-item search search_shop" <if condition="$vo.url_type eq 2 ">style="display: none;"</if>>
        <label class="layui-form-label search_name">店铺名称</label>
        <div class="layui-input-block" style="position: relative;">
            <input type="text" name="shop" lay-verify="title" autocomplete="off"   placeholder="请输入搜索内容" class="layui-input search_text" style="width:80%;display: inline-block;" value='<if condition="$vo.type eq 1 ">{$vo.shop_name}</if><if condition="$vo.type eq 2 ">{$vo.goods_name}</if><if condition="$vo.type eq 3 ">{$vo.news_name}</if>'>
            <div id="do_search" style="cursor:pointer;display: inline-block;height: 38px; width: 8%;background-color:#009688; color: #fff;border-radius:2px;border:none; font-size: 15px; text-align: center; line-height: 38px; position: relative; bottom: 10%; " class="">搜索</div>
        </div>
    </div>
    <input type="hidden" name="search_name" id="search_name">
    <div class="layui-form-item search_option" style="display: none;">
        <label class="layui-form-label"></label>
        <div class="layui-input-block select_div">
            <select name='search_id' id='select_search'>
            </select>
        </div>
    </div>

    <div class="layui-form-item jump" <if condition="$vo.url_type eq 1 ">style="display: none;"<elseif condition="$vo.url_type eq 2 "/><else />style="display: none;"</if>>
        <label class="layui-form-label">WEB跳转链接</label>
        <div class="layui-input-block">
            <input type="text" name="url" lay-verify="title" autocomplete="off" placeholder="请输入跳转链接,需要添加链接前缀 (https://) 或 (http://)" class="layui-input" value="{$vo.url}">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">是否显示</label>
        <div class="layui-input-block">
            <input type="radio" name="status" lay-verify="status" autocomplete="off" title="显示"  class="layui-input" value="1" <if condition="$vo.status == 1 || $vo.status == ''">checked</if>>
            <input type="radio" name="status" lay-verify="status" autocomplete="off" title="隐藏"  class="layui-input" value="2" <if condition="$vo.status == 2">checked</if>>
        </div>
    </div>
    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn" lay-submit="*" lay-filter="*">立即提交</button>
            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
        </div>
    </div>
</form>
<div id="search-item" style="position:fixed;width: 62%;background-color: #eee;z-index:50;top: 50%;left: 20.3%;">

</div>
<script id="pic" type="text/html">
    {{#  layui.each(d.pics, function(index, item){ }}
    <img class="layui-upload-img" src="{{ item }}" style="width: 100px; height: 100px;">
    {{#  }); }}
</script>
<script src="__PUBLIC__/layui/layui.js" charset="utf-8"></script>
<script src="__PUBLIC__/layui/layui-mz-min.js" charset="utf-8"></script>
<!-- 注意：如果你直接复制所有代码到本地，上述js路径需要改成你本地的 -->
<script>
    var pics = [{$vo.pics_arr}];
    layui.use(['form','upload', 'layedit', 'laytpl'], function(){
        var form = layui.form
            ,layer = layui.layer
            ,$ = layui.jquery
            ,upload = layui.upload
            ,layedit = layui.layedit
            ,laytpl = layui.laytpl;
//            layui.selImg($, '#select_search');
        var getTpl = pic.innerHTML
            ,view = document.getElementById('view');

        $('#do_search').click(function(e){
            var keyword = $('.search_text').val();
            if(keyword == ''){
                 layer.msg('关键字不能为空');
                 return false;
            }
            var box =$('#search-item');
            var jump_type = $('input[name="type"]:checked').val();
            $("select[name='search_id']").empty();
            $.ajax({
                type:"GET",
                url:"{:U('search')}?keyword="+keyword+"&type="+jump_type,
                dataType:"json",
                contentType: "application/x-www-form-urlencoded; charset=utf-8",
                success:function(res){

//                      $('#select_search').append(res);
//                      $(".search_option").show();
                    $.each(res,function(k,v){
                        var i = "{src:'" +v.img_url +"', css:'height:40px;width:40px;'}";
                        var l = "<option lay-filter='osearch' value=" + v.id +" img="+'"'+""+i+""+'"'+">"
                                +v.title
                                +"</option>";
                        $('#select_search').append(l);


                    });
                    form.render('select');
                    $(".search_option").show();
                }
            })
        });
        form.on('radio(url_type)', function(data){
            var type = data.value;
            if(type == 1){
                var jump_type = $('input[name="type"]:checked').val();
                if(jump_type == 1){
                    $('.search_name').text('店铺名称');
                }else if(jump_type == 2){
                    $('.search_name').text('商品名称');
                }else if(jump_type == 3){
                    $('.search_name').text('资讯名称');
                }
                $('.search').show();
                $('.jump_type').show();
                $('.jump').hide();
            }else{
                $('.search').hide();
                $('.jump_type').hide();
                $('.jump').show();
            }
        });

        form.on('radio(type)', function(data){
            var jump_type = data.value;
            if(jump_type == 1){
                $('.search_name').text('店铺名称');
            }else if(jump_type == 2){
                $('.search_name').text('商品名称');
            }else if(jump_type == 3){
                $('.search_name').text('资讯名称');
            }
            $('.search_option').hide();
            $('.search_text').val('');
        });

        layedit.set({
            uploadImage: {
                url: '{:U("Home/Public/upload1")}'
            }
        });
        var contIdx = layedit.build("content", {
            tool: ["strong","italic","underline","del","|","left","center","right","|","face","image"]
        });



        var uploadInst = upload.render({
            elem: '#test1'
            ,url: '{:U("Home/Public/upload")}'
            ,before: function(obj){
                //预读本地文件示例，不支持ie8
                obj.preview(function(index, file, result){
                    $('#demo1').attr('src', result).show(); //图片链接（base64）
                });
            }
            ,done: function(res){
                //如果上传失败
                if(res.err_code > 0){
                    return layer.msg('上传失败');
                }else{
                    $("#imgurl").val("https://www.58zltc.com/Uploads/"+res.file_name);
                }
                //上传成功
            }
            ,error: function(){
                //演示失败状态，并实现重传
                var demoText = $('#demoText');
                demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-mini demo-reload">重试</a>');
                demoText.find('.demo-reload').on('click', function(){
                    uploadInst.upload();
                });
            }
        });
        //监听提交
        form.on('submit(*)', function(data){
            var post_data = data.field;
            post_data.content = layedit.getContent(contIdx);
            $.post("{:U('doAddShopAdv')}", post_data, function(e) {
                layer.alert(e.info,function(index){
                    if(e.status == 1){
                        parent.location.reload();
                    }else{
                        layer.close(index);
                    }
                });
            });
            return false;
        });
        laytpl(getTpl).render({pics:pics}, function(html){
            view.innerHTML = html;
        });
    });
</script>
<script>
    function sthis(id,title){
        document.getElementById('search-item').style.display="none";
        var goodsid = document.getElementById('goodsid')
        goodsid.value=id;
        document.getElementById('goods').value= title;
    }
</script>
</body>
</html>